package com.wanlian.service.impl;

import com.wanlian.common.PageCondition;
import com.wanlian.dao.TaskDao;
import com.wanlian.dao.UserDao;
import com.wanlian.dao.UserTaskDao;
import com.wanlian.entity.Task;
import com.wanlian.entity.User;
import com.wanlian.entity.UserTask;
import com.wanlian.model.ResultModel;
import com.wanlian.service.TaskServices;
import com.wanlian.vo.TaskVo;
import org.bouncycastle.LICENSE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TaskServicesImpl implements TaskServices {

    @Autowired
    private TaskDao taskDao;

    @Autowired
    private UserTaskDao userTaskDao;

    @Autowired
    private UserDao userDao;

    @Override
    public ResultModel queryTasks(PageCondition pageCondition) {
        return taskDao.queryTaskByCondition(pageCondition);
    }

    @Override
    public ResultModel doDeleteById(Task task) {
        ResultModel resultModel = new ResultModel(true);
        List<UserTask> userTask = userTaskDao.queryUserTaskByTaskId(task.getId());
        Task dbtask = taskDao.queryById(task.getId());
        if (userTask.size() <=0) {
            if (dbtask != null) {
                dbtask.setEnable(false);
                this.taskDao.update(dbtask);
                resultModel.setBresult(true);
                resultModel.setMessage("删除成功！");
            }else{
                resultModel.setBresult(false);
                resultModel.setMessage("删除失败，暂无任务！");
            }
        } else {
            resultModel.setMessage("该任务下还有用户任务，无法删除！");
        }
        return resultModel;
    }

    @Override
    public ResultModel doEnableById(Task task) {
        ResultModel resultModel = new ResultModel(true);
        Task dbtask = taskDao.queryById(task.getId());
        dbtask.setEnable(true);
        taskDao.update(dbtask);
        resultModel.setMessage("启用成功！");
        return resultModel;

    }

    @Override
    public ResultModel doAdd(Task task) {
        ResultModel resultModel = new ResultModel();
        List<Task> tasks = taskDao.queryTasksByName(task.getName());
        if (task.getRank() < 0) {
            resultModel.setBresult(false);
            resultModel.setMessage("积分应为正整数");
        }
        if (tasks.size() == 0) {
            taskDao.add(task);
            resultModel.setMessage("发布成功");
            resultModel.setBresult(true);
        } else {
            resultModel.setBresult(false);
            resultModel.setMessage("该任务已发布");
        }
        return resultModel;
    }

    @Override
    public ResultModel doUpdateById(Task task) {
        Task tasks = this.taskDao.queryById(task.getId());
        if (tasks != null) {
            if (tasks.isEnable()) {
                tasks.setName(task.getName());
                tasks.setRank(task.getRank());
                tasks.setRankType(task.getRankType());
                this.taskDao.update(tasks);
                ResultModel resultModel = new ResultModel(true);
                resultModel.setMessage("修改成功");
                return resultModel;
            } else {
                ResultModel resultModel = new ResultModel(false);
                resultModel.setMessage("修改失败");
                return resultModel;
            }
        } else {
            ResultModel resultModel = new ResultModel(false);
            resultModel.setMessage("该任务不存在");
            return resultModel;
        }
    }

    @Override
    public ResultModel queryUserTasks(PageCondition pageCondition) {
        return taskDao.queryUserTaskByCondition(pageCondition);
    }

    @Override
    public ResultModel delUserTask(UserTask userTask) {
        return null;
    }

    @Override
    public ResultModel addUserTask(UserTask userTask) {
        return null;
    }

    @Override
    public ResultModel updUserTask(UserTask userTask) {
        return null;
    }

    @Override
    public Task queryTasksById(int id) {
        return taskDao.queryById(id);
    }

    @Override
    public Task queryTasksByName(String name) {
        return taskDao.queryTaskByName(name);
    }

    @Override
    public ResultModel queryAllUserTask(PageCondition pageCondition) {
        return userTaskDao.queryUserTaskByParams(pageCondition);
    }
}
